//
//  ZYLFindBaseViewController.m
//  Cartoon
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLFindBaseViewController.h"
#import "ZYLFindBackCollectionView.h"
#import "Define.h"
#import "ZYLSearchBaseViewController.h"
#import "ZYLTopic_listsMixedModel.h"
#import "ZYLBannerModel.h"
#import "ZYLFindBanners.h"
#import "ReadViewController.h"
#import "ZYLFastlookAllContentViewController.h"
#import "ZYLAllTopicsViewController.h"
#import "ZYLFastLookTableViewCell.h"
#import "ZYLFindBanners.h"
@interface ZYLFindBaseViewController ()<PushToOtherViewController>
@property (nonatomic, retain) ZYLFindBackCollectionView *findBackCollectionView;
@property (nonatomic, retain) NSMutableArray *topic_List;
@property (nonatomic, retain) NSArray *bannerArr;
@property (nonatomic, retain) UIView *stick;
@property (nonatomic, retain) UILabel *titleName;
@property (nonatomic, retain) NSMutableArray *colors;
@property (nonatomic, retain) NSMutableArray *emojiText;
@property (nonatomic, retain) UIButton *searchButton;
@property (nonatomic, retain) UIView *headerTitle;
@property (nonatomic, retain) NSMutableArray *searchIcon;
@end

@implementation ZYLFindBaseViewController

- (void)dealloc
{
    [_findBackCollectionView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.translucent = NO;
    UIColor *temp = self.stick.backgroundColor;
    NSString *tempName = self.titleName.text;
    
    for (; 1; ) {
        NSInteger i = arc4random() % 3;
        NSInteger e = arc4random() % self.emojiText.count;
        
        self.stick.backgroundColor = self.colors[i];
        self.titleName.textColor = self.stick.backgroundColor;
        self.titleName.text = [NSString stringWithFormat:@"今日发现  %@", self.emojiText[e]];
        [self.searchButton setImage:[UIImage imageNamed:self.searchIcon[i]] forState:UIControlStateNormal];
        if (temp != self.stick.backgroundColor && ![tempName isEqualToString:self.titleName.text]) {
            
            break;
        }
    }
}
-(void)loadView
{
    [super loadView];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.translucent = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发现";
    
    self.colors = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:0.9333 green:0.4471 blue:0.4392 alpha:1], [UIColor colorWithRed:0.2 green:0.6549 blue:0.9137 alpha:1], [UIColor colorWithRed:1.0 green:0.698 blue:0.4196 alpha:1], nil];
    self.searchIcon = [NSMutableArray arrayWithObjects:@"iconfont-searchvc-0.png", @"iconfont-searchvc-1.png", @"iconfont-searchvc-2.png", nil];
    self.emojiText = [NSMutableArray arrayWithObjects:@"(´ﾟДﾟ`)", @"(=ﾟ∀ﾟ)=", @"(|||ﾟДﾟ)", @"Σ( ﾟдﾟ)", @"(ノﾟ∀ﾟ)ノ", @"(￣∇￣)", @"(ﾟДﾟ≡ﾟДﾟ)", @"(;´Д`)",@"(つд⊂)", @"(＾o＾)ﾉ", @"|ー`)", @" ﾟ∀ﾟ)σﾟ ∀ﾟ)ノ", @"( ´_ゝ`)", nil];
    [self createView];
    
    self.navigationController.navigationBar.translucent = NO;
    [self getBanner];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToTopic:) name:@"ZYLFindPushToTopic" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToComic:) name:@"ZYLFindPushToComic" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToAllTopic:) name:@"ZYLFindPushToAllTopic" object:nil];
}

- (void)createView
{
    self.headerTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.stick = [[[UIView alloc] initWithFrame:CGRectMake(13, 26.5, 5, 22)] autorelease];
    self.headerTitle.backgroundColor = [UIColor colorWithRed:0.975 green:0.975 blue:0.975 alpha:1];
    self.stick.backgroundColor = self.colors[0];
    self.stick.layer.cornerRadius = 2.5;
    UIView *stickShadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 22)];
    stickShadow.layer.cornerRadius = 2.5;
    stickShadow.layer.borderWidth = 1.5;
    stickShadow.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
    [self.stick addSubview:stickShadow];
    
    [self.headerTitle addSubview:self.stick];
    [self.view addSubview:self.headerTitle];
    self.titleName = [[[UILabel alloc] initWithFrame:CGRectMake(25, 25, 250, 25)] autorelease];
    self.titleName.text = @"今日发现";
    self.titleName.font = [UIFont systemFontOfSize:18];
    [self.headerTitle addSubview:self.titleName];
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchButton.frame = CGRectMake(SCREEN_WIDTH - 35, 25, 25, 25);
    [self.searchButton addTarget:self action:@selector(findRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.headerTitle addSubview:self.searchButton];
    
    UIView *shadowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 63.5, SCREEN_WIDTH, 0.5)] autorelease];
    shadowView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self.headerTitle addSubview:shadowView];
    [self.view bringSubviewToFront:shadowView];
}




- (void)pushToTopic:(NSNotification *)noti
{
    ZYLFastlookAllContentViewController *allVC = [[[ZYLFastlookAllContentViewController alloc] init] autorelease];
    allVC.ID = noti.object;
    [self.navigationController pushViewController:allVC animated:YES];
    
}
- (void)pushToComic:(NSNotification *)noti
{
    ReadViewController *readVC = [[[ReadViewController alloc] init] autorelease];
    readVC.ID = noti.object;
    readVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:readVC animated:YES];
}
- (void)pushToAllTopic:(NSNotification *)noti
{
    ZYLAllTopicsViewController *allTopic = [[[ZYLAllTopicsViewController alloc] init] autorelease];
    [self.navigationController pushViewController:allTopic animated:YES];
}

#pragma mark - 搜索按钮
- (void)findRightButtonAction
{
    ZYLSearchBaseViewController *searchVC = [[[ZYLSearchBaseViewController alloc] init] autorelease];
    UINavigationController *navi = [[[UINavigationController alloc] initWithRootViewController:searchVC] autorelease];
    [self presentViewController:navi animated:YES completion:^{
        
    }];
}

- (void)createBackCollectionView
{
    self.findBackCollectionView = [[[ZYLFindBackCollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)] autorelease];
    self.findBackCollectionView.topic_List = self.topic_List;
    self.findBackCollectionView.bannerArr = (NSMutableArray *)self.bannerArr;
    [self.view addSubview:self.findBackCollectionView];
    void(^block)(void) = ^(void){
        self.findBackCollectionView.banners.pushDelegate = self;
    };
    self.findBackCollectionView.block = block;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)getBanner
{
    [ZYLBannerModel resultWithURLBlock:^(NSMutableArray *arr) {
        self.bannerArr = arr;
        [self getData];
    }];
}

- (void)getData
{
    [ZYLTopic_listsMixedModel Topic_listsMixedWithblock:^(NSMutableArray *array) {
        self.topic_List = array;
        [self createBackCollectionView];
        
    }];
}

- (void)PushToOtherViewControllerWithType:(NSInteger)type andModel:(ZYLBannerModel *)model
{
    if (type == 2) {
        ZYLFastlookAllContentViewController *allContentVC = [[[ZYLFastlookAllContentViewController alloc] init] autorelease];
        allContentVC.ID = [NSNumber numberWithInteger:[model.value integerValue]];
        [self.navigationController pushViewController:allContentVC animated:YES];
        
        
    } else if (type == 3) {
        ReadViewController *readVC = [[[ReadViewController alloc] init] autorelease];
        readVC.ID = [NSNumber numberWithInteger:[model.value integerValue]];
        readVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:readVC animated:YES];
    }
}
@end
